// request.js
import Cookies from 'js-cookie';
import axios from 'axios';
const baseURL = '/api';
import { ElMessage } from 'element-plus'
const instance = axios.create({ baseURL });

// 请求拦截器
instance.interceptors.request.use(
    (config) => {
      // 从 Cookie 中获取 Token
      const token = Cookies.get("myToken");
      if (token) {
        // 将 Token 设置到请求头中
        config.headers['LY-Authorization'] = `${token}`;
      }
      return config;
    },
    (error) => {
      ElMessage.error("请求失败");
      return Promise.reject(error);
    }
  );

instance.interceptors.response.use(
    result => {
        if (result.data.code === 1) {

        }
        return result.data;
    },
    err => {
        ElMessage.error("服务器异常")
        return Promise.reject(err);
    }
);

export default instance;